public class Linklist {
}
// 2、无头双向链表实现
class LinkedList {
    static class Node{
        int val;
        Node prev;
        Node next;
        public Node(int val) {
            this.val=val;
        }
    }
    public Node head;
    public Node last;
    //头插法
    public void addFirst(int data){
        Node node=new Node(data);
        if(head==null) {
            last=node;
        } else {
            //node.prev=null;
            node.next=head;
            head.prev=node;
        }
        head=node;
    }
    //尾插法
    public void addLast(int data) {
        Node node=new Node(data);
        if(head==null) {
            head=node;
        } else {
            last.next=node;
            node.prev=last;
        }
        last=node;
    }
    //任意位置插入,第一个数据节点为0号下标
    public boolean addIndex(int index,int data) {
        if(index==0) {
            addFirst(data);
            return true;
        }
        if(index==size()) {
            addLast(data);
            return true;
        }
        if(index<0||index>size()) {
            throw new ArrayIndexOutOfBoundsException("下标错误");
        }
        Node cur=head;
        Node node=new Node(data);
        while (index!=0) {
            cur=cur.next;
            index--;
        }
        Node curPrev=cur.prev;
        curPrev.next=node;
        node.prev=curPrev;
        cur.prev=node;
        node.next=cur;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        for (Node cur=head; cur!=null; cur=cur.next) {
            if(cur.val==key) {
                return true;
            }
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        for (Node cur=head; cur!=null; cur=cur.next) {
            if(cur.val==key) {
               Node curPrev=cur.prev;
               curPrev.next=cur.next;
               cur.next.prev=curPrev;
               cur.next=null;
               cur.prev=null;
            }
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key) {

    }
    //得到单链表的长度
    public int size() {
        int size=0;
        Node cur=head;
        while (cur!=null) {
            size++;
            cur=cur.next;
        }
        return size;
    }
    //打印单链表
    public void display() {
        Node cur=head;
        for (int i = 0; i < size(); i++) {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
    public void clear() {
        Node cur=head;
        while (cur!=null) {
            Node curNext=cur.next;
            cur.prev=null;
            cur.next=null;
            cur.val=-1;
            cur=curNext;
        }
        last=null;
    }

    public static void main(String[] args) {
        LinkedList p1=new LinkedList();
        p1.addLast(1);
        p1.addLast(2);
        p1.addLast(3);
        p1.addLast(4);
        p1.addLast(5);
        p1.addLast(6);
        p1.display();
        p1.addIndex(5,88);
        p1.display();
        p1.addIndex(0,88);
        p1.display();
    }

}
class ListNode {

    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
   static   ListNode head;
    public static ListNode partition(ListNode pHead, int x) {
        // write code here
        //小于的结点放在前面
        ListNode newMin=null;
        ListNode min=null;
        ListNode newMax=null;
        ListNode max=null;
        ListNode cur=pHead;
        while (cur!=null) {
            if(cur.val<x) {
                if(min==null) {
                    newMin=cur;
                    min=cur;
                }else {
                    min.next=cur;
                    min=min.next;
                }
            }else {
                if(max==null) {
                    newMax=cur;
                    max=cur;
                }else {
                    max.next=cur;
                    max=max.next;
                }
            }
            cur=cur.next;
        }
        if(min!=null) {
            min.next=newMax;
            max.next=null;
            return newMin;
        }else {
            return newMax;
        }

    }

    public static void main(String[] args) {
        ListNode a1=new ListNode(1);
        ListNode a2=new ListNode(3);
        ListNode a3=new ListNode(2);
        ListNode a4=new ListNode(5);
        head=a1;
        head.next=a2;
        a2.next=a3;
        a3.next=a4;
        partition(head, 3);
       ListNode cur= head;
        while (cur!=null) {
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
        System.out.println();
    }
}